<?php

namespace app\api\controller;

use app\api\model\Result;
use Firebase\JWT\JWT;
use think\Controller;
use think\Db;

class BaseController extends Controller
{
    use Result;
    protected $userInfo;
    protected $request;
    protected function initialize()
    {
        parent::initialize();
        $jwt =   $this->request->header('x-token');
        $this->verifyJwt($jwt);

    }
    //校验jwt权限API
    protected function verifyJwt($jwt='')
    {
        $key = md5('nobita');
        // JWT::$leeway = 3;
        try {
            $token = JWT::decode($jwt, $key, array('HS256'));
            $jwtAuth = json_encode($token);
            $this->userInfo = $token;
            $authInfo = json_decode($jwtAuth, true);
            $msg = [];
            if (!empty($authInfo['user_id'])) {
               return true;
            } else {
                return true;
            }
            return $msg;
        } catch (\Exception $e) {
            $this->encode_json(1002,'token无效');
            exit;
        } catch (\Exception $e) {
            $this->encode_json(1003,'token过期');
            exit;
        } catch (\Exception $e) {
            return $e;
        }
    }
}
